package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.DepartmentManagement;
import com.example.prison.entity.vo.DepartmentManagementVO;
import com.example.prison.service.DepartmentManagementService;
import com.example.prison.mapper.DepartmentManagementMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* @author jiang
* @description 针对表【department_management】的数据库操作Service实现
* @createDate 2024-06-03 10:42:06
*/
@Service
public class DepartmentManagementServiceImpl extends ServiceImpl<DepartmentManagementMapper, DepartmentManagement>
    implements DepartmentManagementService{


    @Autowired
    private DepartmentManagementMapper departmentManagementMapper;

    @Override
    public PageInfo<DepartmentManagement> get(int pageNumber, int pageSize, String departName, String departLevel) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(departmentManagementMapper.get(departName,departLevel));
    }

    @Override
    public int add(DepartmentManagement department) {
        String name = department.getDepartName();
        if (ObjectUtils.isNotEmpty(departmentManagementMapper.getOneByName(name))) {
            return -1;
        } else {
            departmentManagementMapper.add(department);
            return 1;
        }
    }

    @Override
    public int delete(Integer[] ids) {
        for(Integer id : ids) {
            DepartmentManagement depart = departmentManagementMapper.getById(id);
            String nextLevel = "";
            if(depart.getDepartLevel() == "一级") {
                nextLevel = "二级";
            } else if(depart.getDepartLevel() == "二级") {
                nextLevel = "三级";
            } else if(depart.getDepartLevel() == "三级") {
                nextLevel = "四级";
            } else if(depart.getDepartLevel() == "四级") {
                nextLevel = "五级";
            }
            if(ObjectUtils.isNotEmpty(departmentManagementMapper.getByLevel(nextLevel))) {
                List<DepartmentManagement> list = departmentManagementMapper.getDepartByLevel(nextLevel);
                for(DepartmentManagement i : list) {
                    if(Objects.equals(depart.getDepartName(), i.getFatherDepart())) {
                        return -1;
                    }
                }
            }
        }
        departmentManagementMapper.deleteDepartment(ids);
        return 1;
    }

    @Override
    public int update(DepartmentManagement department, Integer departId) {
        DepartmentManagement departDTO = departmentManagementMapper.getById(departId);
        departmentManagementMapper.updateDepartment(department, departId);
        if (departmentManagementMapper.getListOneByName(department.getDepartName()).size() > 1) {
            departmentManagementMapper.updateDepartment(departDTO, departId);
            return -1;
        }
        return 1;
    }

    @Override
    public DepartmentManagement getById(Integer departId) {
        return departmentManagementMapper.getById(departId);
    }


    @Override
    public PageInfo<DepartmentManagementVO> getTree(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<DepartmentManagementVO> oneList = departmentManagementMapper.getByLevel("一级");
        List<DepartmentManagementVO> twoList = departmentManagementMapper.getByLevel("二级");
        List<DepartmentManagementVO> threeList = departmentManagementMapper.getByLevel("三级");
        List<DepartmentManagementVO> fourList = departmentManagementMapper.getByLevel("四级");
        for (DepartmentManagementVO department : oneList) {
            List<DepartmentManagementVO> list = new ArrayList<>();
            for (DepartmentManagementVO childDepartment : twoList) {
                if(Objects.equals(department.getDepartName(), childDepartment.getFatherDepart())) {
                    list.add(childDepartment);
                }
            }
            department.setChild(list);
        }
        for (DepartmentManagementVO department : twoList) {
            List<DepartmentManagementVO> list = new ArrayList<>();
            for (DepartmentManagementVO childDepartment : threeList) {
                if(Objects.equals(department.getDepartName(), childDepartment.getFatherDepart())) {
                    list.add(childDepartment);
                }
            }
            department.setChild(list);
        }
        for (DepartmentManagementVO department : threeList) {
            List<DepartmentManagementVO> list = new ArrayList<>();
            for (DepartmentManagementVO childDepartment : fourList) {
                if(Objects.equals(department.getDepartName(), childDepartment.getFatherDepart())) {
                    list.add(childDepartment);
                }
            }
            department.setChild(list);
        }
        return new PageInfo<>(oneList);
    }

}




